package com.wchc.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig  {


    @Bean(name = "redisTemplate")
    public <K, V> RedisTemplate<K, V> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(
                LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL,
                JsonTypeInfo.As.WRAPPER_ARRAY);
        //jackson序列化默认存在不认识字段报错，用户可以自己设置将其忽略
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //================================= RedisTemplate 序列化设置 start ==============================
        RedisTemplate<K, V> template = new RedisTemplate<K, V>();
        template.setConnectionFactory(redisConnectionFactory);

        //key、hashKey采用String的序列化方式
        //template.setKeySerializer(new GenericToStringSerializer(Object.class));
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        //value、HashValue采用Jackson的序列化方式
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));

        //初始化函数，使用以上设置的序列化参数
        template.afterPropertiesSet();
        //设置Redis事务一致支持，可配合@Transactional使用(需要手工关闭连接的谨慎使用)
        template.setEnableTransactionSupport(true);
        //================================= RedisTemplate 序列化设置 end ================================
        return template;
    }

    @Bean(name = "stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);

        //key、value采用String的序列化方式
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
        stringRedisTemplate.setStringSerializer(new StringRedisSerializer());

        //初始化函数，使用以上设置的序列化参数
        stringRedisTemplate.afterPropertiesSet();
        //设置Redis事务一致支持，可配合@Transactional使用(需要手工关闭连接的谨慎使用)
        stringRedisTemplate.setEnableTransactionSupport(true);
        return stringRedisTemplate;
    }




}